<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 仓库类
 * @package  module/base/wms
 * @author   allen.qiang
 * @version 1.0
 */

class wmsStore {

    /**
     * 查询仓库信息
     * @param array $arg_array
     * @return null
     */
    public function searchStore( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //仓库ID
        if (isset($arg_array['storeID']) && is_numeric($arg_array['storeID'])) {
            $sql .= ' AND storeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['storeID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS wms_store FROM cust_deliver ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //查询分类信息
        $search_sql = 'SELECT * FROM wms_store ' . $sql;
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'storeID' => $item->storeID,
                'storeName' => $item->storeName,
                'storeNo' => $item->storeNo,
                'provinceID' => $item->provinceID,
                'provinceName' => $item->provinceName,
                'cityID' => $item->cityID,
                'cityName' => $item->cityName,
                'regionID' => $item->regionID,
                'regionName'=>$item->regionName,
                'address'=>$item->address,
                'priority'=>$item->priority,
                'linkman'=>$item->linkman,
                'mobile'=>$item->mobile,
                'phone'=>$item->phone,
                'postCode'=>$item->postCode,
                'isdefault'=>$item->isdefault,
                'description'=>$item->description
            );
        }

        return $item_array;

    }

    /**
     * 查询单个仓库详情
     * @param int $storeID 仓库ID
     * @return null|array
     */
    public function searchStoreDetail( $storeID ) {

        if (!isset( $storeID ) || !quanli_is_int( $storeID )) {
            return null;
        }

        $args_array = array(
            'storeID' => $storeID
        );
        $store_array = $this->searchStore( $args_array );
        if ($store_array) {
            return $store_array[0];
        } else {
            return null;
        }

    }


    /**
     * 查询当前用户管理的仓库信息
     * @param array $arg_array
     * @return null
     */
    public function searchMaster( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //userID
        if (isset($arg_array['userID']) && !empty($arg_array['userID'])) {
            $sql .= ' AND userID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['userID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS wms_store FROM user_master ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //查询分类信息
        $search_sql = 'SELECT * FROM user_master ' . $sql;
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'userID' => $item->userID,
                'trueName' => $item->trueName,
                'userPwd' => $item->userPwd,
                'email' => $item->email,
                'useFlg' => $item->useFlg,
                'mobile' => $item->mobile,
                'phone' => $item->phone,
                'storeID' => $item->storeID,
                'cityID'=>$item->cityID,
                'distributionID'=>$item->distributionID,
                'departmentID'=>$item->departmentID,
                'extend_1'=>$item->extend_1,
                'extend_2'=>$item->extend_2,
                'extend_3'=>$item->extend_3,
                'extend_4'=>$item->extend_4,
                'extend_5'=>$item->extend_5,
                'extend_6'=>$item->extend_6,
                'extend_7'=>$item->extend_7,
                'extend_8'=>$item->extend_8,
            );
        }

        return $item_array;

    }

}